﻿Imports System.Data.OleDb

Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("PhonesConn").ConnectionString)
            Dim sql As String = "SELECT * FROM Profile WHERE User_Name=@f1"
            Dim cmd As New OleDbCommand(sql, conn)


            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()

                If Not IsDBNull(dr("First_Name")) Then tb_FName.Text = dr("First_Name")
                If Not IsDBNull(dr("Last_Name")) Then tb_LName.Text = dr("Last_Name")
                If Not IsDBNull(dr("DOB")) Then tb_DOB.Text = dr("DOB")
                If Not IsDBNull(dr("Gender")) Then ddl_Gender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
                If Not IsDBNull(dr("Picture")) Then userImage.ImageUrl = dr("Picture")
                If Not IsDBNull(dr("Facebook")) Then tb_Facebook.Text = dr("Facebook")
                If Not IsDBNull(dr("Mobile_Interest")) Then tb_LName.Text = dr("Last_Name")
                If Not IsDBNull(dr("Last_Name")) Then tb_Interest.Text = dr("Mobile_Interest")
                If Not IsDBNull(dr("Address1")) Then tb_Address.Text = dr("Address1")
                If Not IsDBNull(dr("Post_Code")) Then tb_PostCode.Text = dr("Post_Code")
                If Not IsDBNull(dr("Title")) Then ddl_Title.SelectedValue = dr("Title")
            Else
                userImage.ImageUrl = "/Pictures/DefaultUser.JPEG"
            End If
            dr.Close()
            cmd.Dispose()
            conn.Close()
            conn.Dispose()

        End If



    End Sub

    Protected Sub btn_Save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Save.Click
        Dim newFileLoc As String = ""
        Dim sql As String

        'Dim newFileLoc As String = ""

        If fup_Picture.HasFile Then
            newFileLoc = Request.MapPath("pictures")
            newFileLoc = newFileLoc & "/" & fup_Picture.FileName
            fup_Picture.SaveAs(newFileLoc)
        Else
            ' Return
        End If


        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("PhonesConn").ConnectionString)

        If fup_Picture.HasFile Then
            sql = "UPDATE Profile SET First_Name=@f1, Last_Name=@f2, DOB=@f3, Gender=@f6, Description=@f7, Picture=@f8, Facebook=@f10, Mobile_Interest=@f11, Address1=@f12, Post_Code=@13, Title=@14 WHERE User_Name=@f9"
        Else
            sql = "UPDATE Profile SET First_Name=@f1, Last_Name=@f2, DOB=@f3, Gender=@f6, Description=@f7, Facebook=@f10, Mobile_Interest=@f11, Address1=@f12, Post_Code=@13, Title=@14 WHERE User_Name=@f9"
        End If

        Dim cmd As New OleDbCommand(sql, conn)

        conn.Open()
        cmd.Parameters.AddWithValue("@f1", tb_FName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_DOB.Text)
        cmd.Parameters.AddWithValue("@f6", ddl_Gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", tb_Description.Text)
        If fup_Picture.HasFile Then cmd.Parameters.AddWithValue("@f8", "Pictures/" & fup_Picture.FileName)
        cmd.Parameters.AddWithValue("@f10", tb_Facebook.Text)
        cmd.Parameters.AddWithValue("@f11", tb_Interest.Text)
        cmd.Parameters.AddWithValue("@f12", tb_Address.Text)
        cmd.Parameters.AddWithValue("@f13", tb_PostCode.Text)
        cmd.Parameters.AddWithValue("@f14", ddl_Title.SelectedValue)

        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)

        cmd.ExecuteNonQuery()
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)
    End Sub


End Class